package com.ruoyi.system.controller;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.annotation.UserContext;
import com.ruoyi.system.domain.TbProductPackage;
import com.ruoyi.system.domain.TbProductPackageAssociation;
import com.ruoyi.system.domain.vo.CommonData;
import com.ruoyi.system.domain.vo.ProductQuantity;
import com.ruoyi.system.domain.vo.UserData;
import com.ruoyi.system.service.ITbProductPackageService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;

import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.utils.JwtUtils.parseToken;


/**
 * 套餐管理Controller
 *
 * @author ruoyi
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/package")
public class TbProductPackageController extends BaseController
{
    @Autowired
    private ITbProductPackageService tbProductPackageService;


    //     创建 RestTemplate 实例
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 查询关联表
     */
//    @RequiresPermissions("system:package:list")
    @GetMapping("/association/{packageId}")
    public TableDataInfo association(@PathVariable("packageId") int packageId)
    {
//        startPage();
        List<TbProductPackageAssociation> list = tbProductPackageService.selectAssociationList(packageId);
        return getDataTable(list);
    }

    /**
     * 查询套餐管理列表
     */
    @RequiresPermissions("system:package:list")
    @GetMapping("/list")
    public TableDataInfo list(TbProductPackage tbProductPackage)
    {

        startPage();
        List<TbProductPackage> list = tbProductPackageService.selectTbProductPackageList(tbProductPackage);
        return getDataTable(list);
    }


    /**
     * 配置套餐商品
     */
//    @RequiresPermissions("system:package:add")
    @Log(title = "套餐配置", businessType = BusinessType.INSERT)
    @PostMapping("/addPackageProduct")
    public AjaxResult addPackageProduct(@RequestBody CommonData commonData )
    {

        System.out.println(commonData);

        int packageId = commonData.getPackageId();

        List<ProductQuantity> productQuantities = commonData.getProductQuantities();

        return toAjax(tbProductPackageService.configureTbProductPackage(packageId,productQuantities));
    }

    /**
     * 新增套餐管理
     */
    @RequiresPermissions("system:package:add")
    @Log(title = "套餐管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProductPackage tbProductPackage)
    {
        return toAjax(tbProductPackageService.insertTbProductPackage(tbProductPackage));
    }




    /**
     * 导出套餐管理列表
     */
    @RequiresPermissions("system:package:export")
    @Log(title = "套餐管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbProductPackage tbProductPackage)
    {
        List<TbProductPackage> list = tbProductPackageService.selectTbProductPackageList(tbProductPackage);
        ExcelUtil<TbProductPackage> util = new ExcelUtil<TbProductPackage>(TbProductPackage.class);
        util.exportExcel(response, list, "套餐管理数据");
    }

    /**
     * 获取套餐管理详细信息
     */
    @RequiresPermissions("system:package:query")
    @GetMapping(value = "/{packageId}")
    public AjaxResult getInfo(@PathVariable("packageId") Long packageId)
    {
        return success(tbProductPackageService.selectTbProductPackageByPackageId(packageId));
    }



    /**
     * 修改套餐管理
     */
    @RequiresPermissions("system:package:edit")
    @Log(title = "套餐管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProductPackage tbProductPackage)
    {
        return toAjax(tbProductPackageService.updateTbProductPackage(tbProductPackage));
    }

    /**
     * 删除套餐管理
     */
    @RequiresPermissions("system:package:remove")
    @Log(title = "套餐管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{packageIds}")
    public AjaxResult remove(@PathVariable Long[] packageIds)
    {
        return toAjax(tbProductPackageService.deleteTbProductPackageByPackageIds(packageIds));
    }


}
